import configparser
from tkinter import messagebox
from tkinter import *
import subprocess
import time
import shutil
from PIL import Image, ImageTk

# 说明文本位置
xt = 0
yt = 0
# 控件高度
hei1 = 30
# 控件上下间距
hei2 = 2
#按钮上下间距
yb=40
# label
x1 = 20
y1 = 200
# rect
x2 = 100
# sound
x3 = 260
# control
x4 = 420

entry_geo = [0]
entry_sound = [0]
looppro=0
# 创建tkinter窗口实例
wnd = Tk()
# 设置窗口标题
wnd.title('pautowatch')
wnd.geometry(f'800x600+100+20')
Label(wnd, text=r'rect生效数量', fg='blue').place(x=x1, y=y1 - 2 * (hei1 + hei2))
entry_rectnum = Entry(wnd,width=5)
entry_rectnum.place(x=x2, y=y1 - 2 * (hei1 + hei2))
Label(wnd, text=r'确认告警区域号：', fg='orange').place(x=x3, y=y1 - 2 * (hei1 + hei2))
entry_confirm = Entry(wnd,width=6)
entry_confirm.place(x=x3+100, y=y1 - 2 * (hei1 + hei2))
Label(wnd, text='rectname').place(x=x1, y=y1 - (hei1 + hei2) + 5)
Label(wnd, text='geometry').place(x=x2, y=y1 - (hei1 + hei2) + 5)
Label(wnd, text='sound').place(x=x3, y=y1 - (hei1 + hei2) + 5)
Label(wnd, text="rectname列表示监视区域的编号，一共可以监视10个区域。部署时先调整好被监视页面然后点deploy，本窗口会自动最小化不会遮挡被监视页面。\n"
                "rect1监视区域固定用来锚定页面，防止页面错位引起的误判，通常选择被监视页面logo或版本等标识页面的区域。\n"
                "geometry列用来设置所要图像对比监视的区域geometry参数，如200x100+20+30。\n"
                "sound列用来设置对应区域发生变化后告警的语音，如1.mp3。提供了10个预置的语音，可以替换为自己制作的语音文件。\n"
                "sleeptime为对比完各个区域后多少秒开始下一轮对比,如60。\n"
                "confirm用来确认某区域的告警，并在此区域当前状态的基础上继续监视变化。确认告警前先点stop，防止此窗口的覆盖干扰。",
      justify='left', fg='red').place(x=xt, y=yt)
imdon = Image.open('donate.jpg').resize((200,200))
img_jpg = ImageTk.PhotoImage(imdon)
Label(wnd,image = img_jpg).place(x=x4+120,y=y1+110)
Label(wnd,text='随 缘 打 赏\n共 创 辉 煌',fg='blue',font=('华文彩云',30)).place(x=x4+120,y=y1)

for i in range(1,11):
    Label(wnd, text='rect'+str(i)).place(x=x1, y=y1 + (i - 1) * (hei1 + hei2))
    entry_geo.append(Entry(wnd))
    entry_geo[i].place(x=x2, y=y1 + (i - 1) * (hei1 + hei2))
    entry_sound.append(Entry(wnd))
    entry_sound[i].place(x=x3, y=y1 + (i - 1) * (hei1 + hei2))
Label(wnd, text='sleeptime', fg='blue').place(x=x1, y=y1 + 10 * (hei1 + hei2))
entry_sleeptime = Entry(wnd,width=5)
entry_sleeptime.place(x=x2, y=y1 + 10 * (hei1 + hei2))
def confirm():
    print('confirm')
    i=int(entry_confirm.get())
    if(i<1 or i>10):
        messagebox.showinfo("提示","请输入正确的区域号:1-10")
        return
    with open("status\\" + str(i) + "_status.txt", 'r') as f:
        if(1==int(f.read())):
            shutil.copy("cropimages\\"+str(i) + ".png", "cropimages\\"+str(i) + "_base.png")
        else:
            messagebox.showinfo("提示", "你输入的区域号无告警，无需确认")
            return
def show():
    wnd.iconify()
    subprocess.Popen('python showrects.py')
def loadconfig():
    conf = configparser.ConfigParser()
    conf.read('config.ini', encoding='utf-8')
    entry_rectnum.delete(0,END)
    entry_rectnum.insert(0,conf.get('rects', 'rectnum'))
    for i in range(1,11):
        entry_geo[i].delete(0,END)
        entry_geo[i].insert(0, conf.get('rects', 'rect_'+str(i)))
        entry_sound[i].delete(0,END)
        entry_sound[i].insert(0, conf.get('sounds', 'sound_'+str(i)))
    entry_sleeptime.delete(0,END)
    entry_sleeptime.insert(0,conf.get('control', 'sleeptime'))
def saveconfig():
    conf = configparser.ConfigParser()
    conf.read("config.ini", encoding="utf-8")
    conf.set("rects", "rectnum", entry_rectnum.get())
    for i in range(1,11):
        conf.set("rects", "rect_"+str(i), entry_geo[i].get())
        conf.set("sounds", "sound_" + str(i), entry_sound[i].get())
    conf.set("control", "sleeptime", entry_sleeptime.get())
    conf.write(open("config.ini", "w"))
def deploy():
    messagebox.showinfo("提示", "点击确定后请等待，会有完成部署弹窗提示消息。\n"
                                "准备部署对比基准base图片！\n"
                                "准备部署对比初始化init图片")
    wnd.iconify()
    time.sleep(2)
    subprocess.Popen('python deploy.py')
    time.sleep(5)
    messagebox.showinfo("提示", "对比基准base和init图片部署完毕！")
def run():
    messagebox.showinfo("提示", "请调整好被监视页面\n"
                                "点击确定后10秒会开始运行！\n"
                                "本页面会自动最小化，不用担心被其遮挡")
    wnd.iconify()
    time.sleep(10)
    global looppro
    looppro=subprocess.Popen('python loopcmp.py')
    print('run')
def stop():
    global looppro
    looppro.terminate()
    print('stop')
def about():
    messagebox.showinfo("about", "作者：张卓\nemail:madesk@126.com\n")
btwid=7
Button(wnd, width=btwid,text='confirm', fg='orange',command= lambda: confirm()).place(x=x4, y=y1 - 2 * (hei1 + hei2))
Button(wnd, width=btwid,text='show', command= lambda: show()).place(x=x4, y=y1+ yb)
Button(wnd, width=btwid,text='reload', command= lambda: loadconfig()).place(x=x4, y=y1+ 2*yb)
Button(wnd, width=btwid,text='save', command= lambda: saveconfig()).place(x=x4, y=y1+ + 3*yb)
Button(wnd, width=btwid,text='deploy', command= lambda: deploy()).place(x=x4, y=y1+ + 4*yb)
Button(wnd, width=btwid,text='run', command= lambda: run()).place(x=x4, y=y1+ + 5*yb)
Button(wnd, width=btwid,text='stop', command= lambda: stop()).place(x=x4, y=y1+ 6*yb)
Button(wnd, width=btwid,text='about', command= lambda: about()).place(x=x4, y=y1+ 7*yb)

loadconfig()
wnd.mainloop()
